package cn.darkhorse.device.service.impl;

import cn.darkhorse.device.mapper.MesRepairCommissionOrganizationMapper;
import cn.darkhorse.device.pojo.MesRepairCommissionOrganization;
import cn.darkhorse.device.service.MesRepairCommissionOrganizationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangtianqi
 * @since 2024-05-29
 */
@Service
public class MesRepairCommissionOrganizationServiceImpl extends ServiceImpl<MesRepairCommissionOrganizationMapper, MesRepairCommissionOrganization> implements MesRepairCommissionOrganizationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRepairCommissionOrganization(MesRepairCommissionOrganization mesRepairCommissionOrganization) {
        LambdaQueryChainWrapper<MesRepairCommissionOrganization> wrapper = lambdaQuery().eq(MesRepairCommissionOrganization::getOrganizationName, mesRepairCommissionOrganization.getOrganizationName());
        if (mesRepairCommissionOrganization.getId()!=null ) {
            if (wrapper.ne(MesRepairCommissionOrganization::getId, mesRepairCommissionOrganization.getId()).count()>0) {
                throw new RuntimeException("单位名称重复");
            }

        }else {
            if (wrapper.count()>0) {
                throw new RuntimeException("单位名称重复");
            }
        }
        return saveOrUpdate(mesRepairCommissionOrganization);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public MesRepairCommissionOrganization view(Long id) {
        return getById(id);
    }

    @Override
    public List<MesRepairCommissionOrganization> listAll(MesRepairCommissionOrganization mesRepairCommissionOrganization) {
        LambdaQueryWrapper<MesRepairCommissionOrganization> queryWrapper = new LambdaQueryWrapper<>();
        if (mesRepairCommissionOrganization != null) {
            if (mesRepairCommissionOrganization.getOrganizationName() != null && !mesRepairCommissionOrganization.getOrganizationName().isEmpty()){
                queryWrapper.like(MesRepairCommissionOrganization::getOrganizationName,mesRepairCommissionOrganization.getOrganizationName());
            }
            if (mesRepairCommissionOrganization.getContact() != null && !mesRepairCommissionOrganization.getContact().isEmpty()){
                queryWrapper.like(MesRepairCommissionOrganization::getContact,mesRepairCommissionOrganization.getContact());
            }
            if (mesRepairCommissionOrganization.getPhone() != null && !mesRepairCommissionOrganization.getPhone().isEmpty()){
                queryWrapper.like(MesRepairCommissionOrganization::getPhone,mesRepairCommissionOrganization.getPhone());
            }
        }
        return list(queryWrapper);
    }
}
